function tab(e, fn){
	var event = e.target;
	var _that = $(event);
	if(event.tagName.toUpperCase() == 'P'){
		var children = _that.next();
		var all = $('.financial-subtab');
		
		//console.log(children.is(':hidden'))
		if(children.is(':hidden')){
			all.each(function (index, cur){
				cur = $(cur)
				
				if(cur.get(0) != children.get(0)){
					cur.hide()
				}else{
					cur.show()
				}
			})
			
		}else{
			children.hide()
		}
	}else if(event.tagName.toUpperCase() == 'SPAN'){
		if(!_that.is('.none')){
			fn(event.dataset)
			_that.parent().hide()
			event.parentNode.previousElementSibling.innerHTML = event.innerHTML
		}
	}
}
		
function setEcharts(handle, data, fn){
	var newTpl = fn(data)
	handle.setOption(newTpl, true)
}


function concat(a1, a2, key){
	var newData = a1.concat(a2);
	var result = {}, r = [];
	newData.forEach(function (cur, index){
		var k = cur[key].replace(/[-|\s]+/g, '');
		if(result.hasOwnProperty(k)){
			var s = result[k];
			result[k] = Object.assign(s, cur);
		}else{
			result[k] = cur
			
		}
	})

	for(var i in result){
		r.push(result[i])
	}
	
	return r;
}


$.fn.tab = function (settings){
	var _self = $(this),
		options = {
			children : '' ,
			curClass : 'cur',
			callback : function (){} 
		};

	jQuery.extend(options, settings);


	var children = _self.find(options.children),
		childrenLength = children.length;

	children.each(function (index){
		var _sub = $(this),
			_index = index;

		_sub.get(0).addEventListener('click', function (){
			if(!_sub.is('.cur')){
				_sub.addClass(options.curClass).siblings(options.children).removeClass(options.curClass);
				options.callback(_sub, _index);
			}
		}, false);
	})
}


function getTime(t){
	t = parseInt(t);
	var time = new Date(t);
	
	var year = time.getFullYear()
	var month = time.getMonth() + 1;
	var day = time.getDate();
	
	var format = year + '-' + month + '-' + day;
	
	return {
		year : year,
		month : month,
		day : day,
		format : format
	}
	
}



/* 时间段函数组 */
function timeStep(e){
	mui('#popover').popover('toggle',g("popover"));
	$(e.target).parent().hide()
}

function changTime(e){
	var _that = e.target
	var timePicker = new mui.DtPicker({
			type: "date",//设置日历初始视图模式    //真正的月份比写的多一个月。  type的类型你还是可以选择date, datetime month time  hour 
			beginDate: new Date(2018, 0, 1, 0, 0),//设置开始日期   
			endDate: new Date(2099, 12, 31, 0, 0),//设置结束日期    //真正的是10.21
			labels: ['年', '月', '日', '时', '分'],//设置默认标签区域提示语 
		})
	
	timePicker.show(function(e) {
		_that.innerText = e.y.text+"-"+e.m.text+"-"+e.d.text;
		$(_that).data('time', e.y.text+"-"+e.m.text+"-"+e.d.text)
	});
}

function modalHide(){
	mui('#popover').popover('hide');
}

$("#okbut").on("tap",function(e){
	var start = $('#starttime').data('time')
	var end = $('#endtime').data('time')
	var url = $('.financial-tab').data('url')

	modalHide();

	if(!start || !end) return false;

	setData = {}
	setData['start_time'] = start
	setData['end_time'] = end
	setData['date_type'] = 'cumstom'

	requireData({
		url: url,
		data: {
			date_type : 'custom',
			start_time : start,
			end_time : end
		}
	})
	
	
})

/* 时间段函数组END */


function changeDate(_data, bool){
	setData = _data;
	var url = $('.financial-tab').data('url')
	bool = bool || false 
	
	if(bool){
		var d = setData;
	}else{
		var d = {
			date_type : 'date'
		};
		
		//var dom1 = document.querySelector('#trend')
		//var dom2 = document.querySelector('.profit-table')
		
		// if(_data.type == 'custom'){
		// 	var curDate = new Date();
		// 	var curTime = curDate.getTime()
		// 	var onDay = 3600 * 24 * 1000;
		// 	
		// 	var end_time = getTime(curTime).format;
		// 	
		// 	var seep = _data.date == 'week' ? 7 : 30;
		// 	
		// 	var start_time = getTime(curTime - (seep * onDay)).format;
		// 	
		// 	d.start_time = start_time
		// 	d.end_time = end_time
		// }else{
			d.date = _data.date
		//}
	}
	
	
	
	requireData({
		url: url,
		data: d
	})
	
}

function storeChange(_data){
	$('.curStore').data('store', _data.id);
	changeDate(setData, true)
}

function sort(_array, sort){
	return Array.prototype.sort.call(_array, function (o, n){
		if(sort == 'asc'){
			return o.sell_number - n.sell_number
		}else{
			return n.sell_number - o.sell_number
		}
	})
}


$(function (){
	/* 门店请求 */
	ajax({
		url : '/api/store/lst',
		data : {
			access_token : acctoken()
		}
	}, function (res){
		var _data = res.data;
		var html = ['<span data-id="">全部门店</span>'];
		if(_data.length > 0){
			_data.forEach(function (cur, index){
				var tpl = '<span data-id="'+ cur['id'] +'">'+ cur['store_name'] +'</span>';
				html.push(tpl)
			})
		}

		$('.curStore').html(html.join(''));

	})
})